function Can_ViewUnit() {
	Can_Array.apply(this, [ "Can_ViewUnit" ]);
	this.links = null;
}
extend_usemid(Can_ViewUnit, Can_Array);

/**
 * 第二个参数是用于一种情况就是比如要绑定players里每个player面的user
 * 则需要第二个参数指定"user",会表里players然后把里面的每个player里的user对象绑定到unit中 不指定责直接绑定每个player
 */
Can_ViewUnit.prototype.relevant = function(links, append) {
	this.links = links;
	for ( var i in this.datas) {
		// 会爆错 this.datas[i].relevant(this.links.datas[i][key]);
		append = append === undefined || append === null ? "" : append;
		this.datas[i].relevant(this.links.datas, "[" + i + "]", append);
	}
}

Can_ViewUnit.prototype.draw = function() {
	for ( var i in this.datas) {
		this.datas[i].draw();
	}
}

// ////////////////////////////////////////////
// ////////////////////////////////////////////
// ////////////////////////////////////////////
// ////////////////////////////////////////////

function ViewUnit_Base() {
	// 位置信息
	this.position = null;
	// 0-可否onclick 1-可否onmove 2-可否ondoublclick ...等等 拓展
	this.op_state = new Array(0, 0, 0);
	// 元素的显示状态
	this.view_state = new Array(0, 0, 0);
	// 优先级 用于鼠标选中的优先级
	this.priority = null;
	//
	this.links = null;
	// 绑定的数据对象
	this.link = null;
	//
	this.link_addr = "";
	// 
	this.link_append = "";
	// 显示的图片是link中字段为link_key_draw的值（一般是imagesrc）
	this.link_key_draw = null;
	// 通过这个值和view_state共同判断是否画此单元
	this.ctrlLink = null;
	this.ctrlLink_key_view = new Array();
	// 用于内嵌递归对象
	this.insides = null;

	// ////
	// player内部的递归绘图
	// ////
}
/**
 * 关联link对象
 */
ViewUnit_Base.prototype.relevant = function(links, addr, append) {
	this.links = links;
	this.link_addr = addr;
	this.link_append = append;
}

ViewUnit_Base.prototype.isValid_Link = function() {
	var _result = true;

	if (this.links !== undefined && this.links !== null) {
		this.link = eval("this[\"links\"]" + this.link_addr + this.link_append);
	}

	if (this.link === undefined || this.link === null
			|| this.link_key_draw === undefined || this.link_key_draw === null
			|| this.link_key_draw === "") {
		_result = false;
	}
	return _result;
}

/**
 * 基类draw方法
 */
ViewUnit_Base.prototype.draw = function() {
	if (this.insides !== undefined && this.insides !== null) {
		// 是Can
		if (this.insides.datas !== undefined && this.insides.datas !== null
				&& this.insides.datas.length > 0) {
			for ( var i in this.insides.datas) {
				this.insides.datas[i].draw();
			}
		} else {
			this.insides.draw();
		}
	}
}

ViewUnit_Base.prototype.isValid_View = function() {
	var _result = false;
	if (this.view_state[0] === 0) {
		if (this.ctrlLink === undefined || this.ctrlLink === null) {
			_result = true;
		} else {
			if (this.ctrlLink.link === undefined || this.ctrlLink.link == null
					|| this.ctrlLink_key_view === undefined
					|| this.ctrlLink_key_view === null
					|| this.ctrlLink_key_view.length === 0) {
				_result = true;
			} else {
				for ( var i in this.ctrlLink_key_view) {
					// 判断条件link的条件键值对,如果是多个键值对 要配置是全满足通过还是只要一个满足就通过 or/and
					// ps：以后再说
					if (this.ctrlLink.link[this.ctrlLink_key_view[i][0]] === this.ctrlLink_key_view[i][1]) {
						_result = true;
					}
				}
			}
		}

	} else {
		_result = false;
	}

	return _result;
}

/**
 * 根据与目标xy的偏移量生成位置信息
 */
ViewUnit_Base.prototype.genPositionByOffset = function(obj, dx, dy, w, h) {
	this.position = obj.position.offsetPosition(dx, dy, w, h);
}

/**
 * 生成位置信息
 */
ViewUnit_Base.prototype.genPosition = function(x, y, w, h) {
	this.position = new Position_Normal(x, y, w, h);
}

ViewUnit_Base.prototype.setPosition = function(position) {
	this.position = position;
}

// ////////////////////////////////////////////
// ////////////////////////////////////////////
// ////////////////////////////////////////////
// ////////////////////////////////////////////

/**
 * 普通对象
 */
function ViewUnit_Normal(type, priority) {
	ViewUnit_Base.apply(this, arguments);
	this.type = type;
	this.priority = new Priority_Normal(priority);
	this.drawable = null;
	// 选择性构造text/image
	this.init();
}
extend_usemid(ViewUnit_Normal, ViewUnit_Base);

ViewUnit_Normal.prototype.init = function() {
	switch (this.type) {
	case 'image':
		this.drawable = new Draw_Image();
		break;
	case 'text':
		this.drawable = new Draw_Text();
		break;
	default:
		break;
	}
}

/**
 * 绘制方法
 */
ViewUnit_Normal.prototype.draw = function() {
	// 判断可视
	if (this.isValid_View()) {
		// 判断是否有link 2个有一个不满足都不做任何操作
		if (this.isValid_Link()) {
			// 基类画图
			ViewUnit_Base.prototype.draw.apply(this, arguments);
			var _type = this.drawable.object_type;
			// 位置传进去
			this.drawable.position = this.position;
			switch (_type) {
			case 'Draw_Image':
				if (this.link[this.link_key_draw] !== undefined
						&& this.link[this.link_key_draw] !== null) {
					this.drawable.drawable.src = this.link[this.link_key_draw];
				}
				break;
			case 'Draw_Text':
				if (this.link[this.link_key_draw] !== undefined
						&& this.link[this.link_key_draw] !== null) {
					this.drawable.drawable = this.link[this.link_key_draw];
				}
				break;
			default:
				break;
			}
			this.drawable.draw();
		}
	}
}

// //////////////////////////////////////////////
// //////////////////////////////////////////////
// //////////////////////////////////////////////
// //////////////////////////////////////////////
// //////////////////////////////////////////////

/**
 * 内部单元对象
 */
function ViewUnit_Inside(type, priority) {
	ViewUnit_Normal.apply(this, arguments);
	this.init();
}
extend_usemid(ViewUnit_Inside, ViewUnit_Normal);

// //////////////////////////////////////////////
// //////////////////////////////////////////////
// //////////////////////////////////////////////
// //////////////////////////////////////////////
// //////////////////////////////////////////////

function ViewConf_Base() {
	this.facts = null;
}

/**
 * 画出界面，一个场景里所有的可视都在这了 这是view层
 */
ViewConf_Base.prototype.draw = function() {
	for ( var i in this.facts.datas) {
		// 不是数组
		if (this.facts.datas[i].datas === undefined
				|| this.facts.datas[i].datas === null) {
			this.facts.datas[i].draw();
		} else if (this.facts.datas[i].datas.length > 0) {
			for ( var k in this.facts.datas[i].datas) {
				this.facts.datas[i].datas[k].draw();
			}
		}
	}
}

// //////////////////////////////////////////////

function ViewConf_Hall() {
	ViewConf_Base.apply(this, arguments);
	this.facts = new Can_Object();
	this.init();
}
extend_usemid(ViewConf_Hall, ViewConf_Base);

ViewConf_Hall.prototype.init = function() {
	// ////
	this.facts.datas["ui_userlist"] = new ViewUnit_Normal("image", 7);
	this.facts.datas["data_rooms"] = new Can_ViewUnit();
	this.facts.datas["data_users_avatar"] = new Can_ViewUnit();
	this.facts.datas["data_users_nickname"] = new Can_ViewUnit();
	// ////
	this.facts.datas["ui_userlist"].genPosition(755, 240, 164, 340);
	this.facts.datas["ui_userlist"].link_key_draw = "imagesrc";
	this.facts.datas["ui_userlist"].link = new UI_Normal(1, "user_list",
			"media/images/ui/hall/user_list_1.jpg");
	// ////
	for ( var i = 0; i < 8; i++) {
		var _unit = new ViewUnit_Normal("image", 3);
		// 主要是定义位置
		_unit.genPosition(760, 255 + (i * 45), 40, 40);
		_unit.link_key_draw = "drawable_useravatar";
		this.facts.datas["data_users_avatar"].add(_unit);
	}
	// ////
	for ( var i = 0; i < 8; i++) {
		var _unit = new ViewUnit_Normal("text", 2);
		// 主要是定义位置
		_unit.genPosition(760, 255 + (i * 45), 40, 40);
		_unit.link_key_draw = "user_nickname";
		this.facts.datas["data_users_nickname"].add(_unit);
	}
	// ////
	for ( var k = 0; k < 4; k++) {
		var _unit = new ViewUnit_Normal("image", 3);
		// 主要是定义位置
		_unit.genPosition(10 + (k * 145), 20, 120, 160);
		_unit.link_key_draw = "drawable_surface";
		// insides
		_unit.insides = new Can_ViewUnit();

		_unit.insides.datas["ui_ready"] = new ViewUnit_Normal("image", 7);
		// 控制是否显示该unit
		_unit.insides.datas["ui_ready"].ctrlLink = _unit;
		_unit.insides.datas["ui_ready"].ctrlLink_key_view[0] = new Array(
				"state", "waiting");
		_unit.insides.datas["ui_ready"].genPositionByOffset(_unit, 6, 124, 44,
				53);
		_unit.insides.datas["ui_ready"].link_key_draw = "imagesrc";
		_unit.insides.datas["ui_ready"].link = new UI_Normal(1, "ui_ready",
				"media/images/ui/player/ready_logo.png");

		this.facts.datas["data_rooms"].add(_unit);
	}
}

// /////////////////////////////////////////////
// /////////////////////////////////////////////
// /////////////////////////////////////////////
// /////////////////////////////////////////////
// /////////////////////////////////////////////

function ViewConf_Room() {
	ViewConf_Base.apply(this, arguments);
	this.facts = new Can_Object();
	this.init();
}
extend_usemid(ViewConf_Room, ViewConf_Base);

ViewConf_Room.prototype.init = function() {
	// ////
	this.facts.datas["ui_startgame"] = new ViewUnit_Normal("image", 7);
	this.facts.datas["ui_unready"] = new ViewUnit_Normal("image", 7);
	this.facts.datas["data_users"] = new Can_ViewUnit();
	this.facts.datas["data_users_nickname"] = new Can_ViewUnit();
	// ////
	this.facts.datas["ui_startgame"].genPosition(650, 450, 60, 28);
	this.facts.datas["ui_startgame"].link_key_draw = "imagesrc";
	this.facts.datas["ui_startgame"].link = new UI_Normal(1, "ui_startgame",
			"media/images/ui/room/ok.png");
	// ////
	this.facts.datas["ui_unready"].genPosition(650, 480, 60, 28);
	this.facts.datas["ui_unready"].link_key_draw = "imagesrc";
	this.facts.datas["ui_unready"].link = new UI_Normal(1, "ui_unready",
			"media/images/ui/room/untrust.png");
	// ////
	var player_position = new Array();
	player_position[0] = new Position_Normal(790, 400, 125, 181);
	player_position[1] = new Position_Normal(790, 200, 125, 181);
	player_position[2] = new Position_Normal(20, 10, 125, 181);
	player_position[3] = new Position_Normal(210, 10, 125, 181);
	player_position[4] = new Position_Normal(400, 10, 125, 181);
	player_position[5] = new Position_Normal(590, 10, 125, 181);
	player_position[6] = new Position_Normal(780, 10, 125, 181);
	player_position[7] = new Position_Normal(20, 200, 125, 181);
	for ( var i = 0; i < 8; i++) {
		var _unit = new ViewUnit_Normal("image", 3);
		// 主要是定义位置
		_unit.setPosition(player_position[i]);
		_unit.link_key_draw = "drawable_playeravatar";
		// insides
		_unit.insides = new Can_ViewUnit();
		_unit.insides.datas["ui_ready"] = new ViewUnit_Normal("image", 7);
		// 控制是否显示该unit
		_unit.insides.datas["ui_ready"].ctrlLink = _unit.link;
		_unit.insides.datas["ui_ready"].ctrlLink_key_view[0] = new Array(
				"state", "ready");
		_unit.insides.datas["ui_ready"].genPositionByOffset(_unit, 6, 124, 44,
				53);
		_unit.insides.datas["ui_ready"].link_key_draw = "imagesrc";
		_unit.insides.datas["ui_ready"].link = new UI_Normal(1, "ui_ready",
				"media/images/ui/player/ready_logo.png");
		this.facts.datas["data_users"].add(_unit);
	}
	// ////
	for ( var i = 0; i < 8; i++) {
		var _unit = new ViewUnit_Normal("text", 2);
		// 主要是定义位置
		_unit.setPosition(player_position[i]);
		_unit.link_key_draw = "user_nickname";
		this.facts.datas["data_users_nickname"].add(_unit);
	}
}

// /////////////////////////////////////////////
// /////////////////////////////////////////////
// /////////////////////////////////////////////
// /////////////////////////////////////////////
// /////////////////////////////////////////////

function ViewConf_Game() {
	ViewConf_Base.apply(this, arguments);
	this.facts = new Can_Object();
	this.init();
}
extend_usemid(ViewConf_Game, ViewConf_Base);

ViewConf_Game.prototype.init = function() {
	// ////
	// ////
	this.facts.datas["ui_cardstack"] = new ViewUnit_Normal("image", 7);
	this.facts.datas["ui_ok"] = new ViewUnit_Normal("image", 7);
	this.facts.datas["ui_cancel"] = new ViewUnit_Normal("image", 7);
	this.facts.datas["ui_allothero"] = new ViewUnit_Normal("image", 7);
	this.facts.datas["ui_chooserole_10"] = new ViewUnit_Normal("image", 7);
	this.facts.datas["data_players"] = new Can_ViewUnit();
	this.facts.datas["data_users_nickname"] = new Can_ViewUnit();
	// ////
	// ////
	this.facts.datas["ui_cardstack"].genPosition(20, 440, 88, 127);
	this.facts.datas["ui_cardstack"].link_key_draw = "imagesrc";
	this.facts.datas["ui_cardstack"].link = new UI_Normal(1, "ui_cardstack",
			"media/images/ui/game/cardstack.png");
	// ////
	this.facts.datas["ui_ok"].genPosition(700, 450, 60, 28);
	this.facts.datas["ui_ok"].link_key_draw = "imagesrc";
	this.facts.datas["ui_ok"].link = new UI_Normal(1, "ui_ok",
			"media/images/ui/game/ok.png");
	// ////
	this.facts.datas["ui_cancel"].genPosition(700, 450, 60, 28);
	this.facts.datas["ui_cancel"].link_key_draw = "imagesrc";
	this.facts.datas["ui_cancel"].link = new UI_Normal(1, "ui_cancel",
			"media/images/ui/game/cancel.png");
	// ////
	this.facts.datas["ui_allothero"].genPosition(700, 450, 60, 28);
	this.facts.datas["ui_allothero"].link_key_draw = "imagesrc";
	this.facts.datas["ui_allothero"].link = new UI_Normal(1, "ui_allothero",
			"media/images/ui/game/allothero_btn.png");
	// ////
	var player_position = new Array();
	player_position[0] = new Position_Normal(790, 400, 125, 181);
	player_position[1] = new Position_Normal(790, 200, 125, 181);
	player_position[2] = new Position_Normal(20, 10, 125, 181);
	player_position[3] = new Position_Normal(210, 10, 125, 181);
	player_position[4] = new Position_Normal(400, 10, 125, 181);
	player_position[5] = new Position_Normal(590, 10, 125, 181);
	player_position[6] = new Position_Normal(780, 10, 125, 181);
	player_position[7] = new Position_Normal(20, 200, 125, 181);
	// ////
	for ( var i = 0; i < 8; i++) {
		var _unit = new ViewUnit_Normal("image", 3);
		// 主要是定义位置
		_unit.setPosition(player_position[i]);
		_unit.link_key_draw = "drawable_useravatar1";
		// insides
		_unit.insides = new Can_ViewUnit();
		_unit.insides.datas["ui_playerbg"] = new ViewUnit_Normal("image", 7);
		_unit.insides.datas["ui_playerbg"].setPosition(_unit.position);
		_unit.insides.datas["ui_playerbg"].link_key_draw = "imagesrc";
		_unit.insides.datas["ui_playerbg"].link = new UI_Normal(1,
				"ui_playerbg", "media/images/ui/player/player_bg.png");
		//
		_unit.insides.datas["user_avatar"] = new ViewUnit_Normal("image", 3);
		_unit.insides.datas["user_avatar"].setPosition(_unit.position);
		_unit.insides.datas["user_avatar"].link_key_draw = "drawable_useravatar";
		_unit.insides.datas["user_avatar"].links = _unit;
		_unit.insides.datas["user_avatar"].link_append = "[\"link\"]";
		//
		this.facts.datas["data_players"].add(_unit);
	}
	// ////
	for ( var i = 0; i < 8; i++) {
		var _unit = new ViewUnit_Normal("text", 2);
		// 主要是定义位置
		_unit.setPosition(player_position[i]);
		_unit.link_key_draw = "user_nickname";
		this.facts.datas["data_users_nickname"].add(_unit);
	}
}
